package com.springboot.sba.D5_MySql.service;

import com.springboot.sba.D5_MySql.dao.GpMapper;
import com.springboot.sba.bean.Gp;
import com.springboot.sba.utils.HttpRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/12/6 10:49
 */
@Component
public class GpCodeService {
    @Autowired
    private GpMapper gpMapper;

    //@PostConstruct
    public void init() {
        //SZ();
        //SH();
    }

    public void SZ() {
        String sz = "https://hq.sinajs.cn/list=sz";
        //深交所主板代码包括000、001开头的两个系列六位编码。
        List<Gp> list = new ArrayList<>();
        for (int i = 1000; i <= 1999; i++) {
            String code="00"+i;
            String result = HttpRequestUtil.httpRequest(sz + code);
            if (result.length() > 30) {
                int s = result.indexOf("\"") + 1;
                int e = result.indexOf(",");
                String name = result.substring(s, e);
                Gp gp = new Gp();
                gp.setCode(code);
                gp.setName(name);
                gp.setType("2");
                list.add(gp);
            }
        }
        for (int i = 1; i <= 999; i++) {
            String code=getCode(i);
            String result = HttpRequestUtil.httpRequest(sz + code);
            if (result.length() > 30) {
                int s = result.indexOf("\"") + 1;
                int e = result.indexOf(",");
                String name = result.substring(s, e);
                Gp gp = new Gp();
                gp.setCode("sz"+code);
                gp.setName(name);
                gp.setType("2");
                list.add(gp);
            }
        }
        if (list.size() > 0) {
            gpMapper.batchInsert(list);
        }

    }

    public void SH() {
        String sh = "https://hq.sinajs.cn/list=sh";
        String url1 = "https://q.stock.sohu.com/hisHq?code=cn_601868&start=20211202&end=20211203";
        //上交所主板代码包括600、601、603、605开头的四个系列六位编码。
        List<Integer> shList = Arrays.asList(600000, 601000, 603000, 605000);
        shList.forEach(x -> {
            List<Gp> query = shQuery(sh, x, "1");
            if (query.size() > 0) {
                System.out.println(x + " " + query.size());
                gpMapper.batchInsert(query);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public List<Gp> shQuery(String url, int start, String type) {
        List<Gp> list = new ArrayList<>();
        for (int i = start; i <= start + 999; i++) {
            String result = HttpRequestUtil.httpRequest(url + i);
            if (result.length() > 30) {
                int s = result.indexOf("\"") + 1;
                int e = result.indexOf(",");
                String name = result.substring(s, e);
                Gp gp = new Gp();
                gp.setCode("" + i);
                gp.setName(name);
                gp.setType(type);
                list.add(gp);
            }
        }
        return list;
    }
    public String getCode(int code){
        String s=""+code;
        if(s.length()==1){
            s="00000"+s;
        }else if(s.length()==2){
            s="0000"+s;
        }else{
            s="000"+s;
        }
        return  s;
    }
}
